<template>
    <l-sidebar class="left-side"></l-sidebar>
    <div class="list">
        <div class="article-list">
            <div v-infinite-scroll="load" class="infinite-list" style="overflow: auto" :infinite-scroll-disabled="disabled">
                <div class="infinite-list-item"
                     id="body-middle"
                     v-for="item in articles"
                     :key="item.id"
                     @click="readArticle(item)">
                    <div class="card-info">
                        <div class="card-title">
                            {{ item.title }}
                        </div>
                        <div class="card-abstr">{{ item.abstraction }}</div>
                        <div class="card-other">
                            <div class="card-author">
                                <el-icon><User/></el-icon>
                                <span style="margin-left: 5px">{{ item.username }}</span>
                            </div>
                            <div class="card-type">
                                <el-icon><Notebook/></el-icon>
                                <span style="margin-left: 5px">{{ item.typeName }}</span>
                            </div>
                            <div class="card-date">
                                <el-icon><Timer/></el-icon>
                                <span style="margin-left: 5px">{{ item.publishTime }}</span>
                            </div>
                            <div class="card-read">
                                <el-icon><View/></el-icon>
                                <span style="margin-left: 5px">{{ item.readCount }}</span>
                            </div>
                        </div>
                    </div>
                </div>
                <el-empty v-if="empty" class="empty" description="啊哦! 暂无文章, 敬请期待!" ></el-empty>
            </div>
            <div class="bottom-tip">
                <p v-if="noMore">已经是我的极限啦o(╥﹏╥)o</p>
            </div>
        </div>
    </div>
    <div class="right-side">
        <div class="r-sidebar">
            <div class="bookshelf">
                <div class="bottom-level">
                    <div class="motto">学而知不足</div>
                    <a href="javascript:;" @click="onGet(0)">
                        <div class="book" id="ar">算<br />法</div>
                    </a>
                    <a href="javascript:;" @click="onGet(1)">
                        <div class="book" id="fe">前<br />端</div>
                    </a>
                    <a href="javascript:;" @click="onGet(2)">
                        <div class="book" id="java">J<br />A<br />V<br />A</div>
                    </a>
                    <a href="javascript:;" @click="onGet(3)">
                        <div class="book" id="cpp">C<br />+<br />+</div>
                    </a>
                    <a href="javascript:;" @click="onGet(4)">
                        <div class="book" id="c">C<br />语<br />言</div>
                    </a>
                    <a href="javascript:;" @click="onGet(5)">
                        <div class="book" id="py">P<br />y<br />t<br />h<br />o<br />n</div>
                    </a>
                    <a href="javascript:;" @click="onGet(6)">
                        <div class="book" id="base">计<br />算<br />机<br />基<br />础</div>
                    </a>
                    <a href="javascript:;" @click="onGet(7)">
                        <div class="book" id="db">数<br />据<br />库</div>
                    </a>
                    <a href="javascript:;" @click="onGet(8)">
                        <div class="book" id="ai">人<br />工<br />智<br />能</div>
                    </a>
                    <a href="javascript:;" @click="onGet(9)">
                        <div class="book" id="ot">其<br />他</div>
                    </a>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
    import vHeader from "./components/Header.vue";
    import LSidebar from "./components/LSidebar.vue";
    import vFooter from "./components/Footer.vue";
    import {ref, computed, onMounted} from 'vue'
    import {User, Notebook, Timer, View} from '@element-plus/icons'
    import { useRouter } from "vue-router";
    import { getArticleList} from "../api";
    import {ElMessage, ElLoading} from "element-plus";
    export default {
        name: "Index",
        components: {
            vHeader,
            LSidebar,
            vFooter,
            User,
            Notebook,
            Timer,
            View
        },
        setup() {
            const router = useRouter();
            const query = ref({
                pageNum: 1,
                pageSize: 5,
                typeId: null,
            });
            const total = ref(0);
            const articles = ref([]);
            const empty = computed(() => {
                return articles.value.length === 0 || total.value === 0;
            });
            const noMore = computed(() => {
                return !empty && articles.value.length >= total.value;
            });
            const disabled = ref(false);

            const initArticle = async () => {
                const loadingInstance = ElLoading.service({});
                getArticleList(query.value).then(res => {
                    if (res.code === 200) {
                        articles.value = res.data.content;
                        total.value = res.data.count;
                    } else {
                        ElMessage.error(res.message);
                    }
                }).finally(
                    loadingInstance.close()
                );
                setTimeout(() => {
                    loadingInstance.close();
                }, 2000)
            };

            onMounted(() => {
                initArticle();
            });

            const onGet = (typeId) => {
                query.value.typeId = typeId;
                query.value.pageSize = 5;
                disabled.value = false;
                initArticle();
            };

            const load = async () => {
                const loadingInstance = ElLoading.service({});
                query.value.pageSize += 3;
                const res = await getArticleList(query.value);
                if (res.code === 200) {
                    articles.value = res.data.content;
                    total.value = res.data.count;
                    if (query.value.pageSize > total.value) {
                        disabled.value = true;
                    }
                } else {
                    ElMessage.error(res.message);
                }
                loadingInstance.close();
            };

            const readArticle = (item) => {
                router.push({
                    name:'Read',
                    params: {
                        id: item.id
                    }
                });
            };

            return {
                noMore,
                disabled,
                articles,
                empty,
                load,
                readArticle,
                onGet,
            }
        }
    }
</script>

<style scoped>
.left-side {
    display: inline-block;
    margin-left: 15px;
    margin-top: 15px;
}
.list {
    display: inline-block;
    position: absolute;
    left: 22vw;
    margin-top: 5px;
}
.right-side {
    z-index: 10;
    display: inline-block;
    position: absolute;
    right: 15px;
    margin-top: 15px;
}
.article-list {
    /*background-color: RGB(235, 235, 245);*/
    border-radius: 10px;
    margin-top: 15px;
    height: 97vh;
    width: 56vw;
}
.infinite-list {
    height: 97vh;
    padding: 0;
    margin: 0;
    list-style: none;
}
.infinite-list .infinite-list-item {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 50px;
    background: var(--el-color-primary-light-9);
    margin: 10px;
    color: var(--el-color-primary);
}
.infinite-list .infinite-list-item + .list-item {
    margin-top: 10px;
}
::-webkit-scrollbar {
    width: 0;
    height: 10px;
    background-color: #ccc;
}

/* 滚动槽 */
::-webkit-scrollbar-track {
    border-radius: 10px;
}

/* 滚动条滑块 */
::-webkit-scrollbar-thumb {
    border-radius: 10px;
    background: #9a9b9c;
}

.bottom-tip {
    text-align: center;
}

#body-middle {
    display: inline-block;
    vertical-align: top;
    width: 54vw;
    height: 20vh;
    min-height: 160px;
    margin-bottom: 2vh;
    font-size: 0;
    background-color: rgba(255, 255, 255, 0.9);
    border-radius: 10px;
}
#body-middle:hover {
    box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
    cursor: pointer;
}
#body-middle:hover .el-image {
    transform: scale(1.2);
}

#body-middle:hover .card-title {
    color: #339999;
}

.card-info {
    display: inline-block;
    width: calc(100% - 40px);
    height: 20vh;
    padding: 0 20px;
    border-top-right-radius: 10px;
    border-bottom-right-radius: 10px;
    font-size: 20px;
    color: #606266;
    vertical-align: top;
}

.card-title {
    width: 98%;
    padding-top: 4vh;
    font-size: 24px;
    color: #303133;
    text-overflow: ellipsis;
    overflow: hidden;
    white-space: nowrap;
}

.card-abstr {
    width: 90%;
    height: 7vh;
    min-height: 50px;
    padding-top: 1vh;
    margin-bottom: 1vh;
    font-size: 18px;
    color: #606266;
    overflow: hidden;
    white-space: normal;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
}

.card-author,
.card-type,
.card-date,
.card-read {
    display: inline-block;
    font-size: 16px;
    color: #909399;
}

.card-type,
.card-date,
.card-read {
    margin-left: 1vw;
}

.bookshelf {
    display: block;
    border-radius: 10px;
    width: 20vw;
    background-color: RGB(235, 235, 245);
    /*width: 100%;*/
    height: 50vh;
    min-height: 275px;
    font-size: 0;
    z-index: 100;
}

.bottom-level {
    width: 100%;
    height: 50vh;
    position: relative;
    background-color: #836f60;
    border-bottom: 8px #81392f solid;
    border-top: 8px #81392f solid;
    /* box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2); */
}

.book {
    display: inline-block;
    position: absolute;
    bottom: 0;
    min-height: 80px;
    font-family: 楷体;
    font-size: 16px;
    color: #fff;
    text-align: center;
    border: 1px rgba(0, 0, 0, 0.3) solid;
    vertical-align: bottom;
    border-radius: 2px;
    box-shadow: 0 8px 8px 0 rgba(0, 0, 0, 0.2);
}
#ar {
    width: 1.8vw;
    height: 93px;
    padding-top: 10px;
    transform: rotate(-5deg);
    margin-left: 4px;
    background-color: rgb(200, 127, 29);
}
#fe {
    width: 1.5vw;
    margin-left: calc(2.5vw + 8px);
    transform: rotate(10deg);
    padding-top: 10px;
    background-color: #80bbe7;
}
#java {
    width: 1.7vw;
    height: 93px;
    padding-top: 10px;
    margin-left: calc(4.5vw + 10px);
    background-color: rgb(200, 200, 169);
}
#cpp {
    width: 1.6vw;
    margin-left: calc(6.2vw + 12px);
    padding-top: 8px;
    background-color: #d1adf8;
}

#c {
    width: 1.5vw;
    padding-top: 5px;
    margin-left: calc(7.8vw + 14px);
    background-color: #ad87cf;
}

#py {
    width: 1.8vw;
    padding-top: 5px;
    margin-left: calc(9.3vw + 14px);
    background-color: #ad87cf;
}
#base {
    width: 1.9vw;
    height: 100px;
    padding-top: 20px;
    margin-left: calc(11.3vw + 10px);
    background-color: rgb(128, 187, 231);
}
#db {
    width: 1.5vw;
    height: 100px;
    padding-top: 20px;
    margin-left: calc(13.2vw + 12px);
    background-color: rgb(151, 231, 133);
}
#ai {
    width: 1.6vw;
    height: 90px;
    padding-top: 20px;
    margin-left: calc(14.7vw + 14px);
    background-color: rgb(255, 207, 248);
}
#ot {
    width: 1.5vw;
    height: 80px;
    padding-top: 20px;
    margin-left: calc(16.3vw + 16px);
    background-color: rgb(112, 127, 233);
}


a {
    text-decoration: none;
    color: #fff;
}
.book:hover {
    color: black;
}
.motto {
    display: inline-block;
    position: absolute;
    width: 100%;
    top: 10vh;
    font-size: 40px;
    font-family: 隶书;
    color: black;
    text-align: center;
}
.empty {
    border-radius: 10px;
    /*margin-top: px;*/
    height: 97vh;
    width: 56vw;
    display: inline-block;
    background-color: rgba(255, 255, 255, 0.9);
}
</style>